//
//  SEForgetViewController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/12.
//

import UIKit

class SEForgetViewController: SEBaseViewController {

    @IBOutlet weak var confirmBtnView: SERegistrationConfirmBtnView! {
        didSet {
            confirmBtnView.title = "确认修改"
            confirmBtnView.btnBgColor = UIColor.init(hex: "03C691")
            confirmBtnView.btnEnableBgColor = UIColor.init(hex: "AEE5D3")
            confirmBtnView.delegate = self
        }
    }
    @IBOutlet weak var phoeTextView: SEAuthTextFildView! {
        didSet {
            phoeTextView.viewType = .account
            phoeTextView.isHiddenLogo = true
        }
    }
    @IBOutlet weak var codeTextView: SEAuthTextFildView! {
        didSet {
            codeTextView.viewType = .code
            codeTextView.isHiddenLogo = true
            codeTextView.delegate = self
        }
    }
    @IBOutlet weak var passwordTextView: SEAuthTextFildView! {
        didSet {
            passwordTextView.viewType = .password
            passwordTextView.isHiddenLogo = true
        }
    }
    @IBOutlet weak var confirmTextView: SEAuthTextFildView! {
        didSet {
            confirmTextView.viewType = .confirmPsd
            confirmTextView.isHiddenLogo = true
        }
    }

    var viewModel = SEAuthViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        // Do any additional setup after loading the view.
    }

    override func setupUI() {
        super.setupUI()

        addEndEditingTap()

        viewModel.msgCodeHandler = { status in
            switch status {
            case .success:
                self.codeTextView.fireTimer()
            case .auth(let msg), .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }

        viewModel.findPsdHandler = { status in
            switch status {
            case .success:
                self.handlerFindPsdSuccess()
            case .auth(let msg), .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

    private func handlerFindPsdSuccess() {
        UserTools.removeAllUserInfo()
        SEHUD.show(message: "修改成功, 请重新登录")
        self.navigationController?.popToRootViewController(animated: true)
    }

}

// MARK: - SERegistrationConfirmBtnViewDelegate

extension SEForgetViewController: SERegistrationConfirmBtnViewDelegate {
    func registrationConfirmBtnViewAction(view: SERegistrationConfirmBtnView) {
        self.view.endEditing(true)
        viewModel.account = phoeTextView.textField.text!
        viewModel.code = codeTextView.textField.text!
        viewModel.password = passwordTextView.textField.text!
        viewModel.confirmPassword = confirmTextView.textField.text!
        requestFindPsd()
    }
}

// MARK: - SEAuthTextFildView

extension SEForgetViewController: SEAuthTextFildViewDelegate {
    func authTextFildViewCodeBtnAction() {
        view.endEditing(true)
        viewModel.account = phoeTextView.textField.text!
        requestMsgCode()
    }
}

// MARK: - APIRequest

extension SEForgetViewController {
    private func requestMsgCode() {
        viewModel.requestMsgCode()
    }

    private func requestFindPsd() {
        viewModel.requestFindPassword()
    }
}
